#include "colordialog.h"
#include "ui_colordialog.h"
#include <QMessageBox>
#include <QTimer>

#define TIMER_TIMEOUT (3*1000)

ColorDialog::ColorDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ColorDialog)
{
    ui->setupUi(this);
    // 移除标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerHandle()));
    connect(ui->btnExit, SIGNAL(clicked()), this, SLOT(close()));
    connect(ui->btn, SIGNAL(clicked()), this, SLOT(start()));
}

void ColorDialog::start(){
    QString str = ui->btn->text();
    if(str == "start"){
        if(timer->isActive()){
            timer->stop();
        }
        index = 1;
        timer->start(TIMER_TIMEOUT);
        ui->btn->setText("stop");
    }
    else{
        if(timer->isActive()){
            timer->stop();
        }
        ui->btn->setText("start");
    }
}

void ColorDialog::timerHandle(){
    // 设置背景
    QPalette palette = this->palette();
    if(index % 3 == 0){
        palette.setColor(QPalette::Background, QColor(255,0,0));
    }
    else if(index % 3 == 1){
        palette.setColor(QPalette::Background, QColor(0,255,0));
    }
    else if(index % 3 == 2){
        palette.setColor(QPalette::Background, QColor(0,0,255));
    }
    this->setPalette(palette);
    index++;
}

ColorDialog::~ColorDialog()
{
    if(timer->isActive()){
        timer->stop();
    }
    delete ui;
}
